Page({
    /**
     * 页面的初始数据
     */
    data: {
        ctx: {},
        position: "front",
        iknow: true,
        temPhoto: "",
        list: [{
            name: '年龄',
            id: 1,
            select: true
        }, {
            name: '性别',
            id: 2,
            select: false
        }, {
            name: '动漫',
            id: 3,
            select: false
        }],
        id: "1",
        agressTxt: "[ 我们将保障您的个人信息安全,不会泄露您的拍摄照片 ]",

        touchS: [0, 0],
        touchE: [0, 0],
        isAge: true,
        ageWith: "0rpx",
        age:"18"


    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if(getApp().checkIsLogin()){
            wx.getSystemInfo({
                success: res => {
                    // console.log(res)
                    this.setData({
                        CaHeight: res.screenHeight - 157 + "px",
                        statusBarHeight: res.statusBarHeight
                    })
                },
            })
            let ctx = wx.createCameraContext()
            this.setData({
                ctx: ctx
            })
            let openid = wx.getStorageSync('ACCESS_TOKEN')
            let nickname = wx.getStorageSync('appUserInfo').nickName
            let avatar = wx.getStorageSync('appUserInfo').avatarUrl
            let unionid = wx.getStorageSync('appUserInfo').union_id
            let base64String = ""
        }else{
            getApp().doLogin()
        }

    },
    exchange(e) {
        let list = JSON.parse(JSON.stringify(this.data.list))

        if (e.currentTarget.dataset.item.id == 1) {
            this.setData({
                isAge: true
            })
        } else {
            this.setData({
                isAge: false
            })
        }


        list.forEach(item => {
            item.select = false
        })
        list.forEach(item => {
            if (item.id == e.currentTarget.dataset.item.id) {
                item.select = true
            }
        })
        this.setData({
            list: list,
            id: e.currentTarget.dataset.item.id
        })
    },
    cancelNotice() {
        this.setData({
            iknow: false
        })
    },
    naviback() {
        wx.navigateBack({
            delta: 1,
        })
    },
    // jumpMXSW() {
    //     wx.navigateTo({
    //         url: '../mxsw/index',
    //     })
    // },
    exchangeJT() {
        // console.log("12")
        let position = this.data.position
        this.setData({
            position: position == 'back' ? 'front' : 'back'
        })
    },
    // 预览
    previewImg() {
        wx.previewImage({
            urls: [this.data.temPhoto],
        })
    },

    // 打开相册
    openXC() {
        wx.chooseImage({
            count: 1,
            sourceType: ["album"],
            sizeType: ['original', 'compressed'],
            success: res1 => {

                this.setData({
                    temPhoto: res1.tempFilePaths[0]
                })
                // console.log(res1)
                let url = res1.tempFilePaths[0]

                wx.getFileSystemManager().readFile({
                    filePath: url, //选择图片返回的相对路径
                    encoding: 'base64', //编码格式
                    success: res => { //成功的回调
                        // console.log('data:image/png;base64,' + res.data)
                        this.setData({
                            src: 'data:image/png;base64,' + res.data
                        })
                        let data = {
                            openid: wx.getStorageSync('ACCESS_TOKEN'),
                            nickname: wx.getStorageSync('appUserInfo').nickName,
                            avatar: wx.getStorageSync('appUserInfo').avatarUrl,
                            unionid: wx.getStorageSync('appUserInfo').union_id,
                            base64String: res.data,
                            // age:'18'
                        }
                        this.getImg(this.data.id, data)

                    }
                })


            }

        })
    },
    getImg(id, data) {
        wx.showLoading({
            title: '解析中...',
        })
        let url = ""
        if (id == "1") {
            url = getApp().rxbh_nl
            data.age = this.data.age || '18'
        }
        if (id == "2") {
            url = getApp().rxbh_xb
        }
        if (id == "3") {
            url = getApp().rxbh_dm
        }

        wx.request({
            url: url,
            method: "POST",
            data: data,
            header: {
                'content-type': 'application/json'
            },
            success: (result => {
                wx.hideLoading()
                // console.log("res", result)
                if (result.data.code == '0') {
                    if (id == "1") {
                        let img = result.data.age_img_url
                        wx.previewImage({
                            urls: [img],
                        })

                    }
                    if (id == "2") {
                        let img = result.data.sex_img_url
                        wx.previewImage({
                            urls: [img],
                        })

                    }
                    if (id == "3") {
                        let img = result.data.cartoon_img_url
                        wx.previewImage({
                            urls: [img],
                        })
                    }



                    // this.jumpMXSW()
                } else {
                    // let msg = result.data.msg
                    wx.showToast({
                        title: "图片中没有人脸！",
                        icon: 'none',
                        duration: 2000
                    })
                }



            }),
            fail: (fail => {
                wx.hideLoading()
                // console.log("err", fail)
            })
        })




    },


    takePhoto() {
        if (this.data.iknow) {
            this.cancelNotice()
            return false
        }
        let ctx = this.data.ctx
        ctx.takePhoto({
            quality: 'normal',
            success: (res) => {
                let url = res.tempImagePath;
                this.setData({
                    temPhoto: res.tempImagePath
                })

                wx.getFileSystemManager().readFile({
                    filePath: url, //选择图片返回的相对路径
                    encoding: 'base64', //编码格式
                    success: res => { //成功的回调
                        // console.log('data:image/png;base64,' + res.data)
                        this.setData({
                            src: 'data:image/png;base64,' + res.data
                        })
                        let data = {
                            openid: wx.getStorageSync('ACCESS_TOKEN'),
                            nickname: wx.getStorageSync('appUserInfo').nickName,
                            avatar: wx.getStorageSync('appUserInfo').avatarUrl,
                            unionid: wx.getStorageSync('appUserInfo').union_id,
                            base64String: res.data
                        }
                        this.getImg(this.data.id, data)

                    }
                })
            }
        })
    },

    touchStart(e) {
        // console.log(e.touches[0].pageX)
        let sx = e.touches[0].pageX
        let sy = e.touches[0].pageY
        this.data.touchS = [sx, sy]
    },
    touchMove(e) {
        let sx = e.touches[0].pageX;
        let sy = e.touches[0].pageY;
        this.data.touchE = [sx, sy]
    },
    touchEnd(e) {
        let start = this.data.touchS
        let end = this.data.touchE
        // console.log(start)
        // console.log(end)
        if (start[0] < end[0] - 25) {
            // console.log('右滑')
            this.setData({
                ageWith: (end[0] + 25) + "rpx"
            })
            if (end[0] < 100) {
                this.setData({
                    age: 10
                })
            } else
            if (end[0] > 328) {
                this.setData({
                    age: 80
                })
            } else {
                let bili = (end[0] - 100) / (328 - 100)
                let age = parseInt(80 * bili) 
                if (age < 10) {
                    age = 10
                }
                if (age > 80) {
                    age = 80
                }
                this.setData({
                    age: age
                })
            }
        } else if (start[0] > end[0] + 25) {
            // console.log('左滑')
            this.setData({
                ageWith: (end[0] - 85) + "rpx"
            })
            if (end[0] < 100) {
                this.setData({
                    age: 10
                })
            } else
            if (end[0] > 328) {
                this.setData({
                    age: 80
                })
            } else {
                let bili = (end[0] - 100) / (328 - 100)
                let age = parseInt(80 * bili) 
                if (age < 10) {
                    age = 10
                }
                if (age > 80) {
                    age = 80
                }
                this.setData({
                    age: age
                })
            }
        } else {
            // console.log('静止')
        }


        // console.log("年龄",this.data.age )
    }

})